//
//  RecommendDetailScrollView.m
//  Happy Kitchen
//
//  Created by mac_chen on 15/3/7.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "RecommendDetailScrollView.h"

@interface RecommendDetailScrollView ()
{
    UILabel *wayListLabel;
}

@end
@implementation RecommendDetailScrollView



- (instancetype)initWithFrame:(CGRect)frame array:(NSMutableArray *)array recommendDetail:(RecommendDetailModel *)recommendDetail
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSub:array recommendDetail:recommendDetail];
    }
    return self;
}

- (void)createSub:(NSMutableArray *)array recommendDetail:(RecommendDetailModel *)recommendDetail
{
    
    //上面大图片
    self.imagePathThImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, self.frame.size.width - 40, 220)];
    self.imagePathThImageView.userInteractionEnabled = YES;
    self.imagePathThImageView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.imagePathThImageView];
    
    //名字
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.imagePathThImageView.frame.origin.x, self.imagePathThImageView.frame.origin.y + self.imagePathThImageView.frame.size.height + 20, self.frame.size.width - 100, 30)];
    self.nameLabel.font = [UIFont systemFontOfSize:20];
    self.nameLabel.backgroundColor = self.imagePathThImageView.backgroundColor;
    [self addSubview:self.nameLabel];
    
    UILabel *listLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.frame.origin.x, self.nameLabel.frame.origin.y + self.nameLabel.frame.size.height + 20, self.frame.size.width - 100, 30)];
    listLabel.text = @"材料清单";
    listLabel.font = [UIFont systemFontOfSize:18];
    listLabel.backgroundColor = self.imagePathThImageView.backgroundColor;
    [self addSubview:listLabel];
    
    //材料清单
    self.fittingRestrictionLabel = [[UILabel alloc] initWithFrame:CGRectMake(listLabel.frame.origin.x, listLabel.frame.origin.y + listLabel.frame.size.height + 5, self.frame.size.width - 40, 0)];
    CGRect fittingFrame = self.fittingRestrictionLabel.frame;
    fittingFrame.size.height = [[self class] heightForfittingRestriction:self.fittingRestrictionLabel.text];
    self.fittingRestrictionLabel.frame = fittingFrame;
    
    self.fittingRestrictionLabel.backgroundColor = self.imagePathThImageView.backgroundColor;
    self.fittingRestrictionLabel.numberOfLines = 0;
    self.fittingRestrictionLabel.font = [UIFont systemFontOfSize:15];
    [self addSubview:self.fittingRestrictionLabel];
    
    //调味清单
    self.methodLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.fittingRestrictionLabel.frame.origin.x, self.fittingRestrictionLabel.frame.origin.y + self.fittingRestrictionLabel.frame.size.height, 350, 0)];
    //    self.methodLabel.backgroundColor = [UIColor blueColor];
    CGRect methodFrame = self.methodLabel.frame;
    methodFrame.size.height = [[self class] heightFormethodLabel:self.methodLabel.text];
    self.methodLabel.frame = methodFrame;
    self.methodLabel.numberOfLines = 0;
    self.methodLabel.font = [UIFont systemFontOfSize:15];
    [self addSubview:self.methodLabel];
    
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.methodLabel.frame.origin.x, self.methodLabel.frame.origin.y + self.methodLabel.frame.size.height - 30, 100, 30)];
    timeLabel.text = @"制作时间";
    timeLabel.font = [UIFont systemFontOfSize:20];
    [self addSubview:timeLabel];
    
    //时间label
    self.timeLengthLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, timeLabel.frame.origin.y, 100, 30)];
    self.timeLengthLabel.backgroundColor = self.imagePathThImageView.backgroundColor;
    [self addSubview:self.timeLengthLabel];
    
    
    wayListLabel = [[UILabel alloc] initWithFrame:CGRectMake(timeLabel.frame.origin.x, timeLabel.frame.origin.y + timeLabel.frame.size.height + 5, 100, 30)];
    wayListLabel.text =@"步骤";
    wayListLabel.font = [UIFont systemFontOfSize:18];
    wayListLabel.backgroundColor = self.imagePathThImageView.backgroundColor;
    [self addSubview:wayListLabel];
    
    

}



-(void)stepCookarray:(NSMutableArray *)array recommendDetail:(RecommendDetailModel *)recommendDetail
{
    //初始化传数组,用数组个数循环铺页面
    for (NSInteger i = 0; i < array.count; i++) {
        
        
        self.imagePathImageView = [[UIImageView alloc] initWithFrame:CGRectMake(wayListLabel.frame.origin.x + 10, (wayListLabel.frame.origin.y + wayListLabel.frame.size.height + 15) + (self.frame.size.width / 3 + 10) * i , self.frame.size.width / 3, self.frame.size.width / 3)];
        self.describeLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.imagePathImageView.frame.size.width + self.imagePathImageView.frame.origin.x) + 10, self.imagePathImageView.frame.origin.y + 40, (self.frame.size.width - self.imagePathImageView.frame.size.width - self.imagePathImageView.frame.origin.x - 35), self.imagePathImageView.frame.size.height / 2)];
        UILabel *stepLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.origin.x + 8, self.imagePathImageView.frame.origin.y + 55, 15, 15)];
        stepLabel.font = [UIFont systemFontOfSize:15.0];
        //步骤为下标加1
        stepLabel.text = [NSString stringWithFormat:@"%ld", i + 1];
        self.describeLabel.font = [UIFont systemFontOfSize:15];
        self.describeLabel.numberOfLines = 0;
        
        [self addSubview:stepLabel];
        [self addSubview:self.imagePathImageView];
        [self addSubview:self.describeLabel];
        
        //由于model是属性,用的时候要重新用数组初始化model,再赋值
        recommendDetail = [array objectAtIndex:i];
        NSURL *url = [NSURL URLWithString:recommendDetail.imagePath];
        [self.imagePathImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
        self.describeLabel.text = recommendDetail.describe;
        
    }

}

//赋值
- (void)setRecommendDetail:(RecommendDetailModel *)recommendDM
{
    _recommendDM = recommendDM;
    
    self.nameLabel.text = recommendDM.name;

    self.fittingRestrictionLabel.text = recommendDM.fittingRestriction;
    self.timeLengthLabel.text = recommendDM.timeLength;
    NSURL *url = [NSURL URLWithString:recommendDM.imagePathThumbnails];
    [self.imagePathThImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    
}

//自定义label高度
+(CGFloat)heightForfittingRestriction:(NSString *)text
{
    CGSize size = CGSizeMake(350, 1000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey:NSFontAttributeName];
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height + 50;
    
}

+ (CGFloat)heightFormethodLabel:(NSString *)text
{
    CGSize size = CGSizeMake(350, 1000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey:NSFontAttributeName];
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height + 50;
    
}









/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
